/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.utilities;

import java.util.ArrayList;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 *
 * @author Dean
 */
public class DCCRDate {

    private DateTime date;
    private String dateText;
    private String dateString;
    private String monthname;
    private ArrayList<String> paymentDays;
    private boolean isCutOff;
    private String dayofweek;
    private int cutoffSize;
    private ArrayList<String> pastDaysAfterCutOff;
    private final DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy");
    private String cutoff, paymentday1, paymentday2, paymentday3, paymentday4, paymentday5, paymentday6, paymentday7;

    public DCCRDate() {
    }

    public DCCRDate(DateTime date) {

        this.date = date;
        setIsCutOff();
        setDayofweek();
        setPastDaysAfterCutOff();
        setCutOfDays(date);
    }

    public DateTime getDate() {
        return date;
    }

    public void setDate(String date) {
        //logger.info("DATE: " + date);
        String[] parsed = date.split("/");
        if (parsed.length == 3) {
            this.date = Utils.stringArrToDateTime(parsed);
            setIsCutOff();
            setDayofweek();
            setCutOfDays(this.date);
        } else if (parsed.length == 1) {
            DateTime dt = new DateTime(2011,Integer.parseInt(parsed[0]),1,12,0,0,0);
            setMonthname(dt.monthOfYear().getAsText());
        }
    }

    public String getMonthname() {
        return monthname;
    }

    public void setMonthname(String monthname) {
        this.monthname = monthname;
    }

    public String getDateText() {
        dateText = date.toString("MMMM dd, yyyy");
        return dateText;
    }

    public String getDateString() {
        dateString = date.toString(dtf);
        return dateString;
    }

    public boolean isIsCutOff() {
        return isCutOff;
    }

    private void setIsCutOff() {
        if (date.getDayOfMonth() % 6 == 0 || date.getDayOfMonth() == date.dayOfMonth().getMaximumValue()) {
            isCutOff = true;
        } else {
            isCutOff = false;
        }
    }

    public ArrayList<String> getPastDaysAfterCutOff() {
        return pastDaysAfterCutOff;
    }

    private void setPastDaysAfterCutOff() {
        pastDaysAfterCutOff = new ArrayList<String>();
        for (int i = 1;; i++) {
            DateTime temp = date.minusDays(i);
            if ((temp.getDayOfMonth() % 6 == 0 && temp.getDayOfMonth() != 30) || temp.getDayOfMonth() == temp.dayOfMonth().getMaximumValue()) {
                break;
            }
            pastDaysAfterCutOff.add(temp.toString(dtf));
        }
        pastDaysAfterCutOff.trimToSize();
    }

    public String getDayofweek() {
        return dayofweek;
    }

    private void setDayofweek() {
        this.dayofweek = date.dayOfWeek().getAsText();
    }

    private void setCutOfDays(DateTime temp) {

        if ((temp.getDayOfMonth() % 6 == 0 && temp.getDayOfMonth() != 30) || (temp.getDayOfMonth() == 30 && temp.dayOfMonth().getMaximumValue() == 30)) {
            paymentday6 = temp.toString(dtf);
            cutoff = paymentday6;
            paymentday5 = temp.minusDays(1).toString(dtf);
            paymentday4 = temp.minusDays(2).toString(dtf);
            paymentday3 = temp.minusDays(3).toString(dtf);
            paymentday2 = temp.minusDays(4).toString(dtf);
            paymentday1 = temp.minusDays(5).toString(dtf);
            cutoffSize = 6;
            //System.out.println(s1 + "\n" + s2 + "\n" + s3 + "\n" + s4 + "\n" + s5 + "\n" + s6);
        } else if (temp.getDayOfMonth() == temp.dayOfMonth().getMaximumValue()) {
            if (temp.getDayOfMonth() == 31) {
                cutoffSize = 7;
                paymentday7 = temp.toString(dtf);
                cutoff = paymentday7;
                paymentday6 = temp.minusDays(1).toString(dtf);
                paymentday5 = temp.minusDays(2).toString(dtf);
                paymentday4 = temp.minusDays(3).toString(dtf);
                paymentday3 = temp.minusDays(4).toString(dtf);
                paymentday2 = temp.minusDays(5).toString(dtf);
                paymentday1 = temp.minusDays(6).toString(dtf);
                //System.out.println(s1 + "\n" + s2 + "\n" + s3 + "\n" + s4 + "\n" + s5 + "\n" + s6 + "\n" + s7);
            } else if (temp.getDayOfMonth() == 28) {
                cutoffSize = 4;
                paymentday4 = temp.toString(dtf);
                cutoff = paymentday4;
                paymentday3 = temp.minusDays(1).toString(dtf);
                paymentday2 = temp.minusDays(2).toString(dtf);
                paymentday1 = temp.minusDays(3).toString(dtf);
                //System.out.println(s1 + "\n" + s2 + "\n" + s3 + "\n" + s4);
            } else if (temp.getDayOfMonth() == 29) {
                cutoffSize = 5;
                paymentday5 = temp.toString(dtf);
                cutoff = paymentday5;
                paymentday4 = temp.minusDays(1).toString(dtf);
                paymentday3 = temp.minusDays(2).toString(dtf);
                paymentday2 = temp.minusDays(3).toString(dtf);
                paymentday1 = temp.minusDays(4).toString(dtf);
                //System.out.println(s1 + "\n" + s2 + "\n" + s3 + "\n" + s4 + "\n" + s5);
            }
        } else {
            setCutOfDays(temp.plusDays(1));
        }
    }

    public ArrayList<String> getPaymentDays() {
        paymentDays = new ArrayList<String>();
        paymentDays.add(paymentday1);
        paymentDays.add(paymentday2);
        paymentDays.add(paymentday3);
        if (paymentday4 != null) {
            paymentDays.add(paymentday4);
        }
        if (paymentday5 != null) {
            paymentDays.add(paymentday5);
        }
        if (paymentday6 != null) {
            paymentDays.add(paymentday6);
        }
        if (paymentday7 != null) {
            paymentDays.add(paymentday7);
        }
        paymentDays.trimToSize();
        return paymentDays;
    }

    public int getCutoffSize() {
        return cutoffSize;
    }

    public String getPaymentday1() {
        return paymentday1;
    }

    public String getPaymentday2() {
        return paymentday2;
    }

    public String getPaymentday3() {
        return paymentday3;
    }

    public String getPaymentday4() {
        return paymentday4;
    }

    public String getPaymentday5() {
        return paymentday5;
    }

    public String getPaymentday6() {
        return paymentday6;
    }

    public String getPaymentday7() {
        return paymentday7;
    }

    public String getCutoff() {
        return cutoff;
    }
}
